<?php

namespace App\Models\Posts;

use App\Models\User\WxUser;
use App\Models\WxChat;
use App\Wen\Utils\Settings;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxComment extends Model
{
    use HasDateTimeFormatter;
//    use SoftDeletes;

    protected $table = 'wx_comment';

    protected static function boot()
    {
        parent::boot();
        static::deleted(function ($model) {
            WxChat::where('expand_type', 102)->where('expand_id', $model->id)->forceDelete();
        });
    }

    public function user()
    {
        return $this->hasOne(WxUser::class, 'id', 'user_id');
    }
    /**
     * 获取该评论的所属文章
     */
    public function post()
    {
        return $this->belongsTo(WxPost::class,'posts_id','id');
    }


    public function getFormatTimeAttribute()
    {
        return format_datetime($this->created_at);

    }

    public function getIsStickyAttribute($value)
    {
        return (bool)$value;

    }

    public function getDeviceAttribute($v)
    {
        if($v){
            return ['app'=>'app', 'h5'=>'h5', 'mp'=>'mp', 'pc'=>'pc'][$v];
        }else{
            return '';
        }
    }

    public function getIpAddressAttribute()
    {
        if($this->country){
            $comment_show = Settings::get('app_ip_comment_show', 3);
            if($comment_show == 0){
                return '';
            }else if($comment_show == 1){
                return ' '.$this->country;
            }else if($comment_show == 2){
                return ' '.$this->province;
            }else if($comment_show == 3){
                return ' '.$this->city;
            }else if($comment_show == 4){
                return ' '.$this->district;
            }
        }
        return '';
    }

}
